<?php
namespace Views;

class PageView
{
	public $status			= "status";
	public $controller 		= "controller";
	public $mode 			= "mode";
	
	public $createAlbum 	= "createAlbum";
	public $listAlbums 		= "listAlbums";
	public $editAlbum 		= "editAlbum";
	public $showAllAlbums 	= "showAll";
	
	public $albumId			= "albumId";
	public $albumYear		= "albumYear";
	public $albumTitle 		= "albumTitle";
	public $albumDescription= "albumDescription";
	public $imageId			= "imageId";
	public $imageDescription= "imageDescription";
	
	public $userId			= "userId";
	public $password		= "password";
	public $repeatPassword	= "repeatPassword";
	public $userName		= "userName";
	public $firstName		= "firstName";
	public $lastName		= "lastName";
	public $email			= "email";
	
	/**
	 * Genererar en HTML-sida
	 * @param $title Titel på HTML-dokumentet
	 * @param $body HTML för body
	 * @return String HTML dokument
	 */
	public function generateHTMLPage($title, $body)
	{
		$html = "
		<!DOCTYPE html>
		<html>
			<head>
				<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
				<title>$title</title>
				<link href='styles.css' rel='stylesheet' type='text/css'>
			</head>
			<body>
				<div id='container'>
				$body
				</div>
			</body>
		</html>";
		
		return $html;
	}
	
	public function generateGalleryPage($title, $body)
	{
		$html = "
		<!DOCTYPE html>
		<html>
			<head>
				<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
				<title>$title</title>
				<link href='styles.css' rel='stylesheet' type='text/css'>
				<script type='text/javascript' src='plugins/lightbox/js/jquery.js'></script>
			    <script type='text/javascript' src='plugins/lightbox/js/jquery.lightbox-0.5.js'></script>
			    <link rel='stylesheet' type='text/css' href='plugins/lightbox/css/jquery.lightbox-0.5.css' media='screen' />
			    <script type='text/javascript'>$(function() { $('#gallery a').lightBox(); });</script>
			</head>
			<body>
				<div id='container'>
				$body
				</div>
			</body>
		</html>";
		
		return $html;
	}
	
	/**
	 * Hämtar befintlig URL
	 * @param $url string
	 */
	public function getPageURL($queries = true)
	{
		$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
		
		// Om ingen query ska finnas med
		if(!$queries)
		{
			$index = strpos($url, "?");
			$url = substr($url, 0, $index);
			return $url;
		}

		return $url;
	}

	/**
	 * Kontrollerar status för en operation. 
	 * Uppdaterar sidan eller skriver ut eventuella felmeddelanden
	 * @param args - valfritt antal argument att lägga till i url:en
	 * @return $html - returnerar html-sträng vid fel
	 */
	public function updatePage($vars, $values)
	{
		$url = $this->getPageUrl(false);
		
		// Returnera sidans url utan queries om inga skickats med 
		if(count($vars) != count($values))
			return $url;
		
		for ($i=0; $i<count($vars); $i++)
		{
			if($i==0)
				$url .= "?".$vars[$i]."=".$values[$i];
			else
				$url .= "&".$vars[$i]."=".$values[$i];
		}
		
		header("Refresh: 0; url=$url");
	}
	
	/**
	 * Returnerar statusmeddelande
	 * @return String $html
	 */
	public function setStatusText($message, $succes = true)
	{	
		if($succes)
			$html = "<p class='success'>$message</p>";
		else
			$html = "<p class='failure'>$message</p>";
			
		return $html;
	}
	
	/**
	 * Kapslar in en sträng med en html-tagg
	 * @param string HTML-tag to use
	 * @param string String to encapsulate
	 */
	public function encapsulateString($tag, $string)
	{
		return "<$tag>$string</$tag>";
	}
	
	/**
	 * Genererar navigationsmeny
	 * @return String HTML-meny
	 */
	public function getMenu()
	{
		$html = "
		<ul class='main-menu'>
			<li><a href='index.php'>Hem</a></li> 
			<li><a href='manageUsers.php'>Hantera användare</a></li>
			<li><a href='manageAlbums.php?".$this->controller."=".$this->createAlbum."'>Skapa nytt album</a></li>
			<li><a href='manageAlbums.php?".$this->controller."=".$this->listAlbums."'>Hantera album</a></li>
			<li><a href='albums.php?".$this->mode."=".$this->showAllAlbums."'>Visa album</a></li>
		</ul>
		";
		
		return $html;
	}
	
	/**
	 * Skriver ut eventuella felmeddelanden 
	 * @param $errors array
	 * @return $html | bool - returnerar html-sträng vid fel, false vid inga fel
	 */
	public function printErrors($errors)
	{
		$html = "";
		
		foreach ($errors as $error)
			$html .= $this->setStatusText($error, false);
		
		return $html;
	}
	
	public function valueIsSet($name)
	{
		if(isset($_POST[$name]))
			return true;
		else
			return false;
	}
	
	public function getValue($name, $isPOST = true)
	{
		if($isPOST)
		{
			if(isset($_POST[$name]))
				return $_POST[$name];
		}
		else
		{
			if(isset($_GET[$name]))
				return $_GET[$name];
		}
		return false;
	}
	
	public function getNumericValue($name, $isPOST = true)
	{
		if($isPOST)
		{
			if(isset($_POST[$name]) && is_numeric($_POST[$name]))
				return $_POST[$name];
		}
		else
		{
			if(isset($_GET[$name]) && is_numeric($_GET[$name]))
				return $_GET[$name];
		}
		return false;
	}
}

?>